<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/23
 * Time: 9:46 AM
 */
namespace app\backend\modules\business\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\business\services\DepartmentService;
use app\common\models\Department;

class DepartmentController extends BaseController
{
    public function index(){
        return view("backend.business.department.index");
    }
    public function getList(){
        $search = request()->search;
        $list = (new DepartmentService())->getList($search);
        return $this->successJson('',$list);
    }

    public function create(){
        $data = request()->params;
        (new DepartmentService())->changeDepartment($data['name'], $data['pid']??0, 0, $data['display_order']);
        return $this->successJson();
    }
    public function update(){
        $data = request()->params;
        $id = $data['id'];
        $model = Department::find($id);
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        (new DepartmentService())->changeDepartment($data['name'], $model['pid'], $id, $data['display_order']);
        return $this->successJson('');
    }
    public function delete(){
        (new DepartmentService())->deleteDepartment(\request()->id);
        return $this->successJson('');
    }
    /**
     * 检索分类
     */
    public function query(){
        $kwd = trim(request()->kwd);
        $list = Department::site()->select(['id','name']);
        if($kwd){
            $list = $list->where('name','like','%'.$kwd.'%');
        }
        $list = $list
            ->orderBy('display_order', 'desc')
            ->take(1000)
            ->get();
        return $this->successJson('',$list);
    }
}